package top.withu.gaof.freehope.service.impl;

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import top.withu.gaof.freehope.dao.CommentDao;
import top.withu.gaof.freehope.model.Comment;
import top.withu.gaof.freehope.service.CommentService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author: gaofan
 * @Date: 2019/10/9 14:01
 * @Copyright: 2019 www.freehope.top Inc. All rights reserved.
 **/
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentDao commentDao;

    @Override
    public void insert(Comment comment) {
        comment.setEnabled(Boolean.TRUE);
        comment.setCreateDate(new Date());
        commentDao.save(comment);
    }

    @Override
    public List<Comment> getByDocId(Integer docId) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        return commentDao.findByDocIdAndIsEnabled(docId, Boolean.TRUE, sort);
    }

    @Override
    public int countAll() {
        return (int)commentDao.count();
    }
}
